﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UGnome
{
    public partial class UGnome : Form
    {
        Plugin Plugin { get; set; }

        public UGnome()
        {
            InitializeComponent();
        }

        public UGnome(Plugin plugin)
        {
            InitializeComponent();
            Plugin = plugin;
            Plugin.ReloadSettings();

            if (Plugin.StartOnConnect)
            {
                chkStartOnConnect.Checked = true;
            }
            else
            {
                chkStartOnConnect.Checked = false;
            }

            if (Plugin.SavePubData)
            {
                chkSavePubData.Checked = true;
            }
            else
            {
                chkSavePubData.Checked = false;
            }

            foreach (string channel in Plugin.PubChannels)
            {
                txtPubChannels.AppendText(channel + Environment.NewLine);
            }

            txtPassword.Text = Plugin.Password;
            txtURL.Text = Plugin.gUrl;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (chkStartOnConnect.Checked)
            {
                Plugin.StartOnConnect = true;
            }
            else
            {
                Plugin.StartOnConnect = false;
            }

            if (chkSavePubData.Checked)
            {
                Plugin.SavePubData = true;
            }
            else
            {
                Plugin.SavePubData = false;
            }

            // clear old public channel list
            Plugin.PubChannels.Clear();
            foreach (string channel in txtPubChannels.Lines)
            {
                if (!String.IsNullOrEmpty(channel))
                {
                    if (checkChannel(channel))
                    {
                        Plugin.PubChannels.Add(channel);
                    }
                    else
                    {
                        MessageBox.Show(channel + " is not a public channel.");
                    }
                }
            }

            Plugin.Password = txtPassword.Text;
            Plugin.gUrl = txtURL.Text;

            Plugin.SaveSettings();

            Close();
        }

        // make sure no private channels are in the public channel list
        public bool checkChannel(string channel)
        {
            if (channel.ToLower() == "clan recruitment")
            {
                return true;
            }

            if (channel.ToLower().StartsWith("op ") || channel.ToLower().StartsWith("clan "))
            {
                return false;
            }

            return true;
        }

    }
}
